package com.example.permission.mapper;

import com.example.common.model.param.UserParam;
import com.example.permission.model.resp.SystemUserVO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author qwer3
 * @version 1.0.0
 * @create 2025/11/16 11:45
 */
@Mapper
public interface SystemUserRoleMapper {

    @Insert("""
insert into sys_user_role (user_id, role_id)
values (#{userId}, #{roleId}) 
ON DUPLICATE KEY UPDATE
    role_id = VALUES(role_id)
""")
    void saveUserRole(@Param("userId") String userId, @Param("roleId") String roleId);

    @Select("""
<script>
SELECT u.*, u.nickname as name, r.role_id, r.name as role
FROM sys_user u 
    left join sys_user_role ur on u.user_id = ur.user_id
    left join sys_role r on ur.role_id = r.role_id
WHERE 1 = 1
  <if test="userId != null and userId != ''">
    AND u.user_id = #{userId}
  </if>
  <if test="username != null and username != ''">
    AND u.username = #{username}
  </if>
  <if test="nickname != null and nickname != ''">
    AND u.nickname LIKE CONCAT('%', #{nickname}, '%')
  </if>
    order by u.create_time desc
</script>
""")
    List<SystemUserVO> querySystemUserByParam(UserParam userParam);
}
